package com.a918.service.impl;

import com.a918.dao.*;
import com.a918.dto.BwgData;
import com.a918.dto.PublishData;
import com.a918.dto.UnionData;
import com.a918.entity.Content;
import com.a918.entity.FrontFcCheck;
import com.a918.entity.FrontMember;
import com.a918.entity.FrontUnion;
import com.a918.service.BackContentService;
import com.a918.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BackContentServiceImp implements BackContentService {

    private ContentMapper contentMapper;
    private UserMapper userMapper;
    private ContentTypeMapper contentTypeMapper;
    private FrontMemberMapper frontMemberMapper;
    private FrontUnionMapper frontUnionMapper;
    private FrontFcCheckMapper frontFcCheckMapper;

    @Autowired
    public BackContentServiceImp(FrontFcCheckMapper frontFcCheckMapper, ContentTypeMapper contentTypeMapper, ContentMapper contentMapper
            , UserMapper userMapper, FrontMemberMapper frontMemberMapper, FrontUnionMapper frontUnionMapper) {
        this.contentMapper = contentMapper;
        this.userMapper = userMapper;
        this.contentTypeMapper = contentTypeMapper;
        this.frontMemberMapper = frontMemberMapper;
        this.frontUnionMapper = frontUnionMapper;
        this.frontFcCheckMapper = frontFcCheckMapper;
    }

    @Override
    public Content getBackContentById(Integer id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertBackContent(Content record) {
        contentMapper.insert(record);
    }

    @Override
    public void updateBackContent(Content record) {
        contentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void deleteById(Integer id) {
        contentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<BwgData> selectMuseumList(@RequestParam Map<String, String> paramMap, String status) {
        int pno = Integer.parseInt(paramMap.get("page"));
        int pageSize = Integer.parseInt(paramMap.get("limit"));
        PageHelper.startPage(pno, pageSize);
        // 1. 开启分页
        List<FrontMember> UserPoList = frontMemberMapper.selectByStatus(status);
        // 3. 封装list到PageInfo对象中自动分页
        PageInfo<FrontMember> userPoPageInfo = new PageInfo<>(UserPoList);
        // 4. 转换为UserVo类型的PageInfo对象
        PageInfo<BwgData> userVoPageInfo = PageUtils.PageInfo2PageInfoVo(userPoPageInfo);
        // 5. 创建需要分页的UserVoList
        List<BwgData> userVoList = new ArrayList<>();
        // 6. 遍历UserPo给UserVoList初始化
        for(FrontMember userpo : UserPoList){
            BwgData uservo = new BwgData();
            uservo.setId(userpo.getId());
            uservo.setUsername(userMapper.selectByPrimaryKey(userpo.getUserid()).getUsername());
            uservo.setName(userpo.getName());
            uservo.setCreattime(userpo.getCreattime());
            uservo.setProvince(userpo.getProvince());
            userVoList.add(uservo);
        }
        for (BwgData uservo : userVoList) {
            userVoPageInfo.getList().add(uservo);
        }
        return userVoPageInfo;
    }

    @Override
    public PageInfo<UnionData> selectUnionList(@RequestParam Map<String, String> paramMap, String status) {
        int pno = Integer.parseInt(paramMap.get("page"));
        int pageSize = Integer.parseInt(paramMap.get("limit"));
        PageHelper.startPage(pno, pageSize);
        // 1. 开启分页
        List<FrontUnion> UserPoList = frontUnionMapper.selectByStatus(status);
        // 3. 封装list到PageInfo对象中自动分页
        PageInfo<FrontUnion> userPoPageInfo = new PageInfo<>(UserPoList);
        // 4. 转换为UserVo类型的PageInfo对象
        PageInfo<UnionData> userVoPageInfo = PageUtils.PageInfo2PageInfoVo(userPoPageInfo);
        // 5. 创建需要分页的UserVoList
        List<UnionData> userVoList = new ArrayList<>();
        // 6. 遍历UserPo给UserVoList初始化
        for (FrontUnion userpo : UserPoList) {
            UnionData uservo = new UnionData();
            uservo.setId(userpo.getId());
            uservo.setName(userMapper.selectByPrimaryKey(userpo.getUserid()).getUsername());
            uservo.setCreattime(userpo.getCreattime());
            userVoList.add(uservo);
        }
        for (UnionData uservo : userVoList) {
            userVoPageInfo.getList().add(uservo);
        }
        return userVoPageInfo;
    }

    @Override
    public List<Content> selectContentWithNoCover() { return contentMapper.selectContentWithNoCover(); }

    @Override
    public PageInfo<PublishData> getContentlist(Map<String, String> paramMap) {
        int pno = Integer.parseInt(paramMap.get("page"));
        int pageSize = Integer.parseInt(paramMap.get("limit"));
        String status = paramMap.get("status");
        PageHelper.startPage(pno, pageSize);
        List<Content> contentList = contentMapper.getContentList(paramMap);
        PageInfo<Content> contentPageInfo = new PageInfo<>(contentList);
        PageInfo<PublishData> res = PageUtils.PageInfo2PageInfoVo(contentPageInfo);
        List<PublishData> publishDatalist = new ArrayList<>();
        for (Content userpo : contentList) {
            PublishData publishData = new PublishData();
            publishData.setId(userpo.getId());
            publishData.setType(contentTypeMapper.selectByPrimaryKey(userpo.getTypeid()).getName());
            publishData.setTitle(userpo.getTitle());
            publishData.setAdduser(userMapper.selectByPrimaryKey(userpo.getAdduserid()).getUsername());
            publishData.setAddtime(userpo.getAddtime());
            publishData.setWeight(userpo.getWeight());
            publishDatalist.add(publishData);
        }
        for (PublishData i : publishDatalist) {
            res.getList().add(i);
        }
        return res;
    }

    @Override
    public void deleteFrontUnionById(Integer id){
        frontUnionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public FrontMember getFrontMemberById(Integer id){
        return frontMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateFrontMember(FrontMember frontMember) { frontMemberMapper.updateByPrimaryKeySelective(frontMember); }

    @Override
    public FrontUnion getFrontUnionById(Integer id) { return frontUnionMapper.selectByPrimaryKey(id); }

    @Override
    public void updateFrontUnion(FrontUnion frontUnion) { frontUnionMapper.updateByPrimaryKeySelective(frontUnion); }

    @Override
    public void insertFrontFcCheck(FrontFcCheck frontFcCheck) { frontFcCheckMapper.insertSelective(frontFcCheck); }

    @Override
    public void delFrontFcCheckByIdType(Integer id, String type){
        frontFcCheckMapper.deleteByTypeId(type,id);
    }

    @Override
    public FrontFcCheck getFrontFcCheckByIdType(Integer id, String type){
        return frontFcCheckMapper.selectByTypeAndCheckid(id,type);
    }
}
